package com.hardroid.workload.ui.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hardroid.workload.R;
import com.hardroid.workload.ui.tablet.DashboardMultiPaneActivity;
import com.hardroid.workload.utils.EmployerAdapter;
import com.hardroid.workload.utils.Singleton;
import com.hardroid.workload.utils.UIUtils;
import com.hardroid.workload.utils.UnitAdapter;
import com.hardroid.workload.workcalc.ObsvProject;
import com.hardroid.workload.workcalc.ObsvProject.ProjectStatus;

public class NewProjectFragment extends Fragment implements OnClickListener{

	private EditText projectNameEdit;
	private EditText obsvNameEdit;
	private EditText companyNameEdit;
	private EditText companyLocationEdit;
	private EditText projectNotesEdit;
	private EditText tallyEdit;
	private EditText obsvGapEdit;
//	private TimePicker workStartEdit;
//	private TimePicker workEndEdit;
	

	private Button cancelButton;
	private Button nextButton;

	ObsvProject newProject;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);		
		Log.i("New Proj Fragment", "onCreate");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.i("New Proj Fragment", "onCreate");
		return inflater.inflate(R.layout.fragment_new_project	, container,false);
		
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		projectNameEdit = (EditText) getView().findViewById(R.id.new_project_project_name_text);
		companyNameEdit = (EditText) getView().findViewById(R.id.new_project_company_name_text);
		companyLocationEdit = (EditText) getView().findViewById(R.id.new_project_company_location_text);
		obsvNameEdit = (EditText) getView().findViewById(R.id.new_project_observer_name_text);
		tallyEdit = (EditText) getView().findViewById(R.id.new_project_tally_text);
		obsvGapEdit = (EditText) getView().findViewById(R.id.new_project_obsv_gap_text);
		projectNotesEdit = (EditText) getView().findViewById(R.id.new_project_project_note_text);
//		workStartEdit= (TimePicker) getView().findViewById(R.id.new_project_work_start);
//		workEndEdit= (TimePicker) getView().findViewById(R.id.new_project_work_end);

		cancelButton = (Button) getView().findViewById(R.id.new_project_cancel_button);
		nextButton = (Button) getView().findViewById(R.id.new_project_next_button);

		cancelButton.setOnClickListener(this);
		nextButton.setOnClickListener(this);
//		
//		workStartEdit.setIs24HourView(true);
//		workStartEdit.setCurrentHour(8);
//		workStartEdit.setCurrentMinute(0);
//		workEndEdit.setIs24HourView(true);
//		workEndEdit.setCurrentHour(17);
//		workEndEdit.setCurrentMinute(0);
		
	}

	public boolean validateField() {
		return !projectNameEdit.getText().toString().equals("")
				&& !companyNameEdit.getText().toString().equals("")
				&& !companyLocationEdit.getText().toString().equals("")
				&& !obsvGapEdit.getText().toString().equals("")
				&& !obsvNameEdit.getText().toString().equals("")
				&& !tallyEdit.getText().toString().equals("");
	}

	public void createProject() {
		newProject = new ObsvProject();
		newProject.editProjectDesc(projectNameEdit.getText().toString(),
				obsvNameEdit.getText().toString(), companyNameEdit.getText()
						.toString(), companyLocationEdit.getText().toString(),
				projectNotesEdit.getText().toString());
		newProject.obsvDesc.setGap(Integer.parseInt(obsvGapEdit.getText()
				.toString()));
		newProject.obsvDesc.setTally(Integer.parseInt(tallyEdit.getText()
				.toString()));		

		
		Singleton.currentProject = newProject;
		Singleton.currentProject.projectStatus = ProjectStatus.PROJECT_NOT_FINISHED;
		Singleton.employerAdapter = new EmployerAdapter(getActivity(), Singleton.currentProject.lEmployer);
		Singleton.unitAdapter = new UnitAdapter(getActivity(), Singleton.currentProject.lWorkUnit);
//		Singleton.generateNewReference(workStartEdit.getCurrentMinute(),workStartEdit.getCurrentHour(), workEndEdit.getCurrentMinute(), workEndEdit.getCurrentHour());
		
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub			
		if (v == cancelButton) {
			getActivity().finish();
		} else if (v == nextButton) {
			if (validateField()) {
				if (newProject == null) {
					createProject();										
					getActivity().finish();					
					boolean saved = Singleton.saveProject(newProject);
					if (saved){											
						 //to dashboard activity
						if (UIUtils.isHoneycombTablet(getActivity())){
							startActivity(new Intent(getActivity(),
									DashboardMultiPaneActivity.class));
						}else{
//							startActivity(new Intent(getActivity(),
//									DashboardActivity.class));	
						}
						
						getActivity().finish();
						
						Toast.makeText(getActivity(),
								"New Project is created..", Toast.LENGTH_SHORT)
								.show();
					}else{
						Toast.makeText(getActivity(),
								"Failed at creating project", Toast.LENGTH_SHORT)
								.show();
					}
					
				}
			} else {
				Toast.makeText(getActivity(),
						"Please insert valid data.", Toast.LENGTH_SHORT).show();
			}
		}
	}
}